<template>
    <div>
        <a-form :form="form" id="update-form">
            <div class="header">
                <h3>一、基本信息</h3>
                <div class="operate">
                    <div v-if="is_admin == 1">
                        <transition name="slide-fade">
                        <a-button type="primary" @click="returned" v-show="!disabled">还原默认</a-button>
                        </transition>
                        <a-button type="primary" v-if="disabled" @click="updateRole">修改</a-button>
                        <a-button type="primary" @click="onSubmit" v-else>保存修改</a-button>
                    </div>
                </div>
            </div>
            <a-row :gutter='10' class="form-main-content">
                <a-col :key="1" :span="8" class="form-item">
                    <a-form-item label="角色编号">
                        {{data.sn}}
                    </a-form-item>
                </a-col>
                <a-col :key="2" :span="8" class="form-item">
                    <a-form-item label="角色名称">
                        <a-input  maxlength="32" placeholder="请输入角色名称" v-decorator="[
                            'name',
                            {
                                initialValue:data.name,
                            }
                        ]" v-if="!disabled"/>
                        <div v-if="disabled">{{data.name}}</div>
                    </a-form-item>
                </a-col>
                <a-col :key="3" :span="8" class="form-item">
                    <a-form-item label="角色类型">
                        {{data.type_name}}
                    </a-form-item>
                </a-col>
            </a-row>
            <a-row :gutter='10' class="form-main-content">
                <a-col :key="4" :span="8" class="form-item">
                    <a-form-item label="角色状态">
                        <div v-if="disabled">{{parseInt(data.status) === 0 ? '停用' : '可用'}}</div>
                        <a-select v-decorator="['status', {initialValue:data.status}]" v-if="!disabled">
                            <a-select-option :value="1">
                                可用
                            </a-select-option>
                            <a-select-option :value="0">
                                停用
                            </a-select-option>
                        </a-select>
                    </a-form-item>
                </a-col>
                <a-col :key="5" :span="8" class="form-item">
                    <a-form-item label="页面权限">
                        <router-link :to="'/role-list/'+id">
                            <a type="primary" class="view-authority">查看</a>
                        </router-link>
                    </a-form-item>
                </a-col>
                <a-col :key="6" :span="8" class="form-item">
                    <a-form-item label="建立时间">
                        {{data.created_at}}
                    </a-form-item>
                </a-col>
                <a-col :key="7" :span="24" class="form-item">
                    <a-form-item label="备注">
                        <a-textarea
                                v-if="!disabled"
                                rows="6"
                                placeholder="请输入备注"
                                v-decorator="[
                                    'descs',
                                    {
                                        initialValue:data.descs,
                                    }]"
                        />
                        <div v-if="disabled">{{data.descs ? data.descs : '无'}}</div>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                is_admin:this.$store.getters.is_admin,
                id: 0,
                form: this.$form.createForm(this),
                disabled: true,
                types: [],
                data: []
            }
        },
        methods: {
            onSubmit(form) {
                this.form.validateFields((err, values) => {
                    if (!err) {
                        this.postData(values).then(res => {
                            if (res.id) {
                                this.$message.success(res.msg, 1);
                                this.data = {...this.data, ...values};
                                this.disabled = true
                            } else {
                                this.$message.error(res.msg, 1);
                            }
                        })
                    }
                })
            },
            updateRole() {
                this.disabled = false
            },
            edit() {
                this.disabled = false;
                let setData = {
                    name: this.data.name,
                    status: this.data.status,
                    descs: this.data.descs
                };
                this.form.setFieldsValue(setData)
            },
            returned() {
                let setData = {
                    name: this.data.name,
                    status: this.data.status,
                    descs: this.data.descs
                };
                this.form.setFieldsValue(setData)
            },
            async postData(param) {
                return await this.http('put', 'role/update?id=' + this.id, param);
            },
            async getDetail(id) {
                return await this.http('get', 'role/view', {id: id});
            }
        },
        mounted() {
            this.id = this.$route.params.id;
            this.getDetail(this.id).then(res => {
                this.data = res
            })
        }
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "../../assets/scss/mixin";

    .header {
        @include fj();
        margin-bottom: 30px;
        .operate {
            .ant-btn + .ant-btn {
                margin-left: 40px;
            }
        }
    }

    .view-authority {
        border-bottom: 2px solid $blue;
        padding: 4px;
        position: relative;
        top: -3px;
    }

    .slide-fade-enter-active {
        transition: all .3s ease;
    }
    .slide-fade-leave-active {
        transition: all .3s ease;
    }
    .slide-fade-enter, .slide-fade-leave-to {
        transform: translateX(20px);
        opacity: 0;
    }
</style>
